package com.ebupt.migu.music.common.auth;

import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.common.util.TokenUtils;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.system.mapper.UserMapper;
import com.google.common.collect.Maps;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2019/9/24
 */
@Component
@Slf4j
public class AuthBaseUtils {

    @Resource
    private UserMapper userMapper;

    private static boolean isRepeat;

    @Value("${system.login.repeat}")
    public void setIsRepeat(boolean isRepeat) {
        AuthBaseUtils.isRepeat = isRepeat;
    }

    /**
     * 获取用户基础信息
     *
     * @param request
     * @return
     */
    public static Map<String, Object> authBaseInfo(HttpServletRequest request) {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        SysUser user = (SysUser) request.getSession().getAttribute(AuthConst.USER_BASE_INFO);
        if (null == user || StringUtils.isBlank(user.getLoginName())) {
            throw new MyException(StatusEnum.USER_VERIFICATION_FAILED, "请先登录系统");
        }
        params.put("userID", user.getId());
        params.put("creatorId", user.getLoginName());
        params.put("creatorName", user.getUserName());
        params.put("creatorDepartmentId", user.getDeptId());
        params.put("creatorDepartment", user.getDeptName());
        params.put("creatorPhone", user.getMobile());
        params.put("creatorEmail", user.getEmail());
        return params;
    }

    /**
     * 获取用户基础信息
     *
     * @param request
     * @return
     */
    public static SysUser getSessionUser(HttpServletRequest request) {
        SysUser user = new SysUser();
        user = (SysUser) request.getSession().getAttribute(AuthConst.USER_BASE_INFO);
        if (null == user || StringUtils.isBlank(user.getLoginName())) {
            throw new MyException(StatusEnum.USER_VERIFICATION_FAILED, "请先登录系统");
        }
        return user;
    }

    public static Map<String, Object> authBaseInfo(List<Object> paramList,HttpServletRequest request){
        Map<String, Object> params = Maps.newHashMap();
        SysUser user = (SysUser) request.getSession().getAttribute(AuthConst.USER_BASE_INFO);
        if (null == user || StringUtils.isBlank(user.getLoginName())) {
            throw new MyException(StatusEnum.USER_VERIFICATION_FAILED, "请先登录系统");
        }
        params.put("userID", user.getId());
        params.put("creatorId", user.getLoginName());
        params.put("creatorName", user.getUserName());
        params.put("creatorDepartmentId", user.getDeptId());
        params.put("creatorDepartment", user.getDeptName());
        params.put("creatorPhone", user.getMobile());
        params.put("creatorEmail", user.getEmail());
        params.put("strategyList",paramList);
        return params;
    }

    /**
     * 通过token获取用户信息
     *
     * @param request 请求信息
     * @return 用户信息
     */
    public SysUser getUserByToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        log.info("当前token：{}", token);
        // 解析token
        Claims claims = TokenUtils.parseJWT(token);

        // 获取loginName
        String loginName = claims.getId();
        log.info("用户登录账号：{}", loginName);
        Map<String, Object> login = new HashMap<>(3);
        login.put("pageStart", 0);
        login.put("rows", 9999);
        login.put("loginName", loginName);
        List<SysUser> loginList = userMapper.queryByPage(login);
        SysUser user = null;
        if (!loginList.isEmpty()) {
            user = loginList.get(0);
            log.info("当前用户信息：{}", user);
        }
        return user;
    }
}
